package cn.stylefeng.guns.modular.basic.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCode;
import cn.stylefeng.guns.modular.basic.model.params.BasicCodeParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCodeResult;
import cn.stylefeng.guns.modular.basic.service.BasicCodeService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;


/**
 * 通用编码控制器
 *
 * @author zhucx
 * @Date 2022-12-23 11:03:31
 */
@Controller
@RequestMapping("/basicCode")
public class BasicCodeController extends BaseController {

    private String PREFIX = "/basic/basicCode";

    @Autowired
    private BasicCodeService basicCodeService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2022-12-23
     */
    @RequestMapping("")
    public String index(Model model) {
        return PREFIX + "/basicCode.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2022-12-23
     */
    @RequestMapping("/add")
    public String add(Model model) {
        return PREFIX + "/basicCode_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2022-12-23
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
        return PREFIX + "/basicCode_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2022-12-23
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicCodeParam basicCodeParam) {
    	User user = userService.getcurrentUser();
    	basicCodeParam.setDeliverId(user.getDeliverId());
    	basicCodeParam.setCreateUser(user.getName());

        if (ToolUtil.isEmpty(basicCodeParam.getPid()) || basicCodeParam.getPid().equals(0L)) {
        	basicCodeParam.setPid(0L);
        	basicCodeParam.setPids("[0],");
        } else {
            Long pid = basicCodeParam.getPid();
            BasicCode temp = basicCodeService.getById(pid);
            String pids = temp.getPids();
            basicCodeParam.setPid(pid);
            basicCodeParam.setPids(pids + "[" + pid + "],");
        }
        
        this.basicCodeService.add(basicCodeParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2022-12-23
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicCodeParam basicCodeParam) {
    	basicCodeParam.setUpdateUser(userService.getcurrentUser().getName());

        if (ToolUtil.isEmpty(basicCodeParam.getPid()) || basicCodeParam.getPid().equals(0L)) {
        	basicCodeParam.setPid(0L);
        	basicCodeParam.setPids("[0],");
        } else {
            Long pid = basicCodeParam.getPid();
            BasicCode temp = basicCodeService.getById(pid);
            String pids = temp.getPids();
            basicCodeParam.setPid(pid);
            basicCodeParam.setPids(pids + "[" + pid + "],");
        }
        this.basicCodeService.update(basicCodeParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2022-12-23
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicCodeParam basicCodeParam) {
    	List<BasicCodeResult> list = basicCodeService.likePids(basicCodeParam.getId());
    	for (BasicCodeResult r : list) {
    		BasicCodeParam p1 = new BasicCodeParam();
    		p1.setId(r.getId());
    		basicCodeService.delete(p1);
		}
        this.basicCodeService.delete(basicCodeParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2022-12-23
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicCodeParam basicCodeParam) {
        BasicCode detail = this.basicCodeService.getById(basicCodeParam.getId());
        BasicCodeResult basicCodeResult = new BasicCodeResult();
        ToolUtil.copyProperties(detail, basicCodeResult);
        
        if(basicCodeResult.getPid().equals(0l)) {
        	basicCodeResult.setPName("顶级");
        } else {
        	BasicCode detail2 = this.basicCodeService.getById(basicCodeResult.getPid());
        	basicCodeResult.setPName(detail2.getCodeName());
        }
        return ResponseData.success(basicCodeResult);
    }

    /**
     * 获取部门的tree列表，ztree格式
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:56 PM
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree(BasicCodeParam basicCodeParam) {
    	basicCodeParam.setDeliverId(userService.getDeliverId());
        List<ZTreeNode> tree = this.basicCodeService.tree(basicCodeParam);
		return tree;
    }

    /**
     * 获取部门的tree列表，ztree格式
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:56 PM
     */
    @RequestMapping(value = "/treeo")
    @ResponseBody
    public List<ZTreeNode> treeo(BasicCodeParam basicCodeParam) {
    	basicCodeParam.setDeliverId(userService.getDeliverId());
        List<ZTreeNode> tree = this.basicCodeService.tree(basicCodeParam);
        tree.add(ZTreeNode.createParent());
		return tree;
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2022-12-23
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicCodeParam basicCodeParam,String condition ) {
    	basicCodeParam.setDeliverId(userService.getDeliverId());
        return this.basicCodeService.findPageBySpec(basicCodeParam,condition);
    }

}


